package com.sloan.community.controller.error;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.reactive.result.view.Rendering;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import static com.sloan.community.common.Constant.ERROR_MSG;

/**
 * @Author: yaosj
 * @CreateTime: 2024-12-16
 */
@Controller
@RequestMapping("err")
public class ErrorController {

    @GetMapping("4xx.html")
    public Mono<Rendering> err4xx(ServerWebExchange exchange, Model model) {
        String msg = String.valueOf(exchange.getAttributes().get(ERROR_MSG));
        if (StringUtils.hasText(msg)) {
            model.addAttribute(ERROR_MSG, msg);
        }
        return Mono.just(Rendering.view("err/4xx").build());
    }

    @GetMapping("5xx.html")
    public Mono<Rendering> err5xx(ServerWebExchange exchange, Model model) {
        String msg = String.valueOf(exchange.getAttributes().get(ERROR_MSG));
        if (StringUtils.hasText(msg)) {
            model.addAttribute(ERROR_MSG, msg);
        }
        return Mono.just(Rendering.view("err/5xx").build());
    }

}
